41223110 cp2023

  • Home
    • SMap
    • reveal
    • blog
  • About
  • editing functions
  • w2
    • 請逐行詳細說明程式用法
  • w4
  • w5
  • w6
    • Flag
    • ROC單一菱形
    • GD 圖形庫中的函數
    • 說明基本 gd 繪圖
  • w7
    • China flag Program Usage
    • Korea flag Program Usage
  • w11
  • w12
  • w13
    • 心得
  • w15
    • 1
    • 2
  • Brython
w7 << Previous Next >> Korea flag Program Usage

China flag Program Usage

五芒星

void draw_star(gdImagePtr img, int x, int y, int size, int color, double rotation_angle) {
    gdPoint points[10];

    // 計算星形的五個外點和五個內點
    double outer_radius = size / 2;
    double inner_radius = size / 6;
    double angle = M_PI / 5.0;

    for (int i = 0; i < 10; i++) {
        double radius = (i % 2 == 0) ? outer_radius : inner_radius;
        double theta = rotation_angle + i * angle;
        points[i].x = x + radius * cos(theta);
        points[i].y = y + radius * sin(theta);
    }

    // 使用 gdImageFilledPolygon 繪製星形
    gdImageFilledPolygon(img, points, 10, color);
}
  1. gdPoint points[10];:創建一個 gdPoint 類型的陣列 points 來存儲星星的十個頂點座標。
  2. double outer_radius = size / 2; 和 double inner_radius = size / 6;:定義星星的外半徑和內半徑。
  3. double angle = M_PI / 5.0;:計算星星的角度,因為五芒星有五個外點和五個內點,所以每個點之間的角度是 360 度除以 5。
  4. for (int i = 0; i &lt; 10; i++) {:進入一個迴圈,迴圈 10 次,分別計算十個頂點的座標。
  5. double radius = (i % 2 == 0) ? outer_radius : inner_radius;:根據 i 的奇偶性,選擇使用外半徑或內半徑。
  6. double theta = rotation_angle + i * angle;:計算頂點的極坐標角度,加上旋轉角度。
  7. points[i].x = x + radius * cos(theta); 和 points[i].y = y + radius * sin(theta);:使用極坐標轉換公式,計算頂點的直角坐標。
  8. gdImageFilledPolygon(img, points, 10, color);:使用 gdImageFilledPolygon 函數繪製填充多邊形,即星星。

這個 draw_star 函數使用數學計算的方式,根據外半徑和內半徑、角度以及旋轉角度,計算星星的十個頂點的座標,然後使用 GD 庫的函數將這些座標連接起來,形成一個填充的星星。


w7 << Previous Next >> Korea flag Program Usage

Copyright © All rights reserved | This template is made with by Colorlib